package com.jnevision.recruitmentplan.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnevision.common.CommonQuery;
import com.jnevision.dept.service.DeptServiceImpl;
import com.jnevision.recruitmentplan.model.RecruitmentPlan;
import com.jnevision.recruitmentplan.service.RecruitmentPlanServiceImpl;

/**
 * <p>
 * 招聘计划 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-18
 */

@RestController
@RequestMapping("/recruitmentplans")
public class RecruitmentPlanController extends ApiController {
	
	
    @Autowired
    private RecruitmentPlanServiceImpl recruitmentPlanServiceImpl;
    @Autowired
    private DeptServiceImpl deptServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody RecruitmentPlan plan) {
    	plan.setValid(true);
        return this.success(this.recruitmentPlanServiceImpl.save(plan));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody RecruitmentPlan plan) {
        return this.success(this.recruitmentPlanServiceImpl.updateById(plan));
    }

    @GetMapping
    public R<RecruitmentPlan> get(int id) {
        return this.success(this.recruitmentPlanServiceImpl.getById(id));
    }

    @DeleteMapping
    public R<Boolean> delete(int id) {
        return this.success(this.recruitmentPlanServiceImpl.removeById(id));
    }

    @GetMapping("/page")
    public R<IPage<RecruitmentPlan>> page(Page<RecruitmentPlan> page, CommonQuery query) {
        IPage<RecruitmentPlan> planPage = this.recruitmentPlanServiceImpl.page(page,
            new QueryWrapper<RecruitmentPlan>().eq(query.getDeptId()!=null&&query.getDeptId() > 0, "dept_id", query.getDeptId())
                .ge(StringUtils.isNotBlank(query.getStart()), "recruitment_time", query.getStart())
                .le(StringUtils.isNotBlank(query.getEnd()), "recruitment_time", query.getEnd())
                .like(StringUtils.isNotBlank(query.getKeyword()), "job", query.getKeyword()));

        planPage.getRecords().forEach(pp -> {
            String deptName = this.deptServiceImpl.getById(pp.getDeptId()).getName();
            pp.setDeptName(deptName);
        });

        return this.success(planPage);
    }
}
